<template>
  <div class="channel-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>频道管理</span>
          <el-button type="primary" @click="handleAdd">添加频道</el-button>
        </div>
      </template>
      
      <el-table :data="channels" v-loading="loading">
        <el-table-column prop="name" label="频道名称" />
        <el-table-column prop="deviceName" label="所属设备" />
        <el-table-column prop="resolution" label="分辨率" />
        <el-table-column prop="frameRate" label="帧率" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'danger'">
              {{ row.status === 'active' ? '活跃' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="handleView(row)">查看</el-button>
            <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const channels = ref([
  {
    id: 1,
    name: '主摄像头',
    deviceName: '前门摄像头',
    resolution: '1920x1080',
    frameRate: '25fps',
    status: 'active'
  },
  {
    id: 2,
    name: '副摄像头',
    deviceName: '后门摄像头',
    resolution: '1280x720',
    frameRate: '15fps',
    status: 'inactive'
  }
])

onMounted(() => {
  loadChannels()
})

const loadChannels = async () => {
  loading.value = true
  try {
    // TODO: 实现频道列表加载逻辑
    console.log('加载频道列表')
  } catch (error) {
    ElMessage.error('加载频道列表失败')
  } finally {
    loading.value = false
  }
}

const handleAdd = () => {
  router.push('/channels/new')
}

const handleView = (row) => {
  router.push(`/channels/${row.id}`)
}

const handleEdit = (row) => {
  router.push(`/channels/${row.id}/edit`)
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除这个频道吗？', '确认删除', {
      type: 'warning'
    })
    // TODO: 实现删除逻辑
    ElMessage.success('删除成功')
    loadChannels()
  } catch {
    // 用户取消删除
  }
}
</script>

<style scoped>
.channel-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>